import random

from django.core.management.base import BaseCommand
from django.db import transaction
from django.contrib.auth.models import User

from map.models import *

class Command(BaseCommand):
    help = "generates new map elements"

    @transaction.commit_on_success
    def handle(self, *args, **options):
        map = Map(min_x = -4, max_x = 4, min_y = -4, max_y = 4, special_x = 0, special_y = 0)
        map.save()
        admin = User.objects.get(id = 1)
        
        for x in xrange(map.min_x, map.max_x):
            for y in xrange(map.min_y, map.max_y):
                if (x == map.special_x) and (y == map.special_y):
                    cell_content_type = CellContentType.objects.get(name = "Settlement")
                    cell_content = CellContent(cell_content_type = cell_content_type)
                    cell_content.save()
                    cell = Cell.objects.create(map = map, name = "Support cell", x = x, y = y, owner = admin, content = cell_content)
                    cell.save()
                else:
                    cell_content_type = CellContentType.get_random_value()
                    cell_content = CellContent(cell_content_type = cell_content_type)
                    cell_content.save()
                    if cell_content_type.name == "Valley":
                        # Country center
                        area_content_type_country = AreaContentType.objects.get(name = "Country")
                        area_content_country = AreaContent(
                            area_content_type = area_content_type_country,
                            cell_content = cell_content
                        )
                        area_content_country.save()
    
                        building_main = Building(
                            building_type = BuildingType.objects.get(name = "Main"), 
                            level = 1
                        )
                        building_main.save()
    
                        slot_content = SlotContent(
                            slot_place = SlotPlace.objects.get(
                                slot_type = SlotType.objects.get(name = "Assembly place slot")
                            ),
                            area_content = area_content_country,
                            content = building_main
                        )
                        slot_content.save()
    
                        # Suburb with resource mines
                        area_content_type_suburb = AreaContentType.objects.get(name = "Suburb")
                        area_content_suburb = AreaContent(
                            area_content_type = area_content_type_suburb,
                            cell_content = cell_content
                        )
                        area_content_suburb.save()
                        
                        for place_num in range(area_content_type_suburb.child_count):
                            building = Building.get_random_content()
                            building.save()
    
                            slot_content = SlotContent(
                                slot_place = SlotPlace.objects.get(
                                   slot_type = SlotType.objects.get(name = "Resource mine slot"),
                                   number = place_num + 1
                                ),
                                area_content = area_content_suburb,
                                content = building
                            )
                            slot_content.save()

                    cell = Cell.objects.create(map = map, x = x, y = y, owner = admin, content = cell_content)
                    cell.save()

